layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'pinvoice_list',
		form_filter = 'pinvoice_search_form',
		const_where = {};
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: invoice_list_url,
			method: 'post',
			height: 'full-200',
			where: {
				_o: 'invoice_id',
				_b: 'desc',
				no_need_proof: 'yes',
				order_shoukuan: [2, 3, 4]
			},
			cols: [invoice_cols],
			page: {
				layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
				limit: 30,
				theme: '#393d49',
				groups: 8
			},
			autoSort: false,
			toolbar: '#pinvoiceToolbar',
			defaultToolbar: [],
			initSort: {
				field: 'invoice_id',
				type: 'desc'
			}
		});
	}
	// 导出请款发票
	var upload_invoice = upload.render({
		elem: '.qingkuanfapiao',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(qingkuanfapiao_uri + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.confirm('文件打包完成', {
						icon: 1,
						title: '温馨提示：',
						btn: ['下载打包文件']
					}, function(index) {
						location.href = data.count
						console.log('下载')
					})
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'daochuchaxun':
				// 导出查询
				send_info(search_export_uri, const_where, function(data) {
					if (data.code == -1) {
						layer.msg(data.msg, {
							icon: 2,
							anim: 6
						});
					} else {
						layer.confirm('文件打包完成', {
							icon: 1,
							title: '温馨提示：',
							btn: ['下载打包文件']
						}, function(index) {
							location.href = data.data
							console.log('下载')
						})
					}
				})
				break;
			case 'downFile':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('尚未选择', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				order_no = new Array();
				for (var i in checkStatus.data) {
					order_no.push(checkStatus.data[i].out_order_no);
				}
				layer.open({
					type: 2,
					content: zip_down_url + '?out_order_no=' + order_no
				});
				break;
			case 'sendfkSMS':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择发送短信的发票', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				order_no = new Array();
				for (var i in checkStatus.data) {
					order_no.push(checkStatus.data[i].order_nos);
				}
				let post_json = {
					order_nos: order_no,
					send_type: 'fukuan'
				};
				send_info(send_invoice_sms_url, post_json, function(data) {
					layer.msg('短信发送成功', {
						icon: 1,
						anim: 6
					});
					setTimeout(function() {
						layer.closeAll();
					}, 3000);
				})
				break;
			case 'applyInvoice':
				var open_url = $(this).attr('data-url');
				layer.open({
					type: 2,
					anim: -1,
					title: '导入收款单',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: open_url
				});
				break;
			case 'applyFukuan':
				var open_url = $(this).attr('data-url');
				layer.open({
					type: 2,
					anim: -1,
					title: '导入付款单',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: open_url
				});
				break;
			case 'saleInvoice':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的发票', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				order_no = new Array();
				for (var i in checkStatus.data) {
					order_no.push(checkStatus.data[i].invoice_id);
				}
				var invoice_ids = order_no.join(',')
				send_info(excel_sale_invoice_url + '?invoice_ids=' + invoice_ids, {}, function(data) {
					layer.closeAll();
					layer.open({
						type: 2,
						content: data.data
					});
					setTimeout(function() {
						layer.closeAll();
					}, 2000);
				})

				break;
			case 'gysInvoice':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的发票', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				order_no = new Array();
				for (var i in checkStatus.data) {
					order_no.push(checkStatus.data[i].invoice_id);
				}
				var invoice_ids = order_no.join(',')
				send_info(excel_gys_invoice_url + '?invoice_ids=' + invoice_ids, {}, function(data) {
					layer.closeAll();
					layer.open({
						type: 2,
						content: data.data
					});
					setTimeout(function() {
						layer.closeAll();
					}, 2000);
				})
				break;
			case 'applyOneInvoice':
				var open_url = $(this).attr('data-url');
				layer.open({
					type: 2,
					anim: -1,
					title: '申请独立开票',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: open_url
				});
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	table.on('row(' + table_filter + ')', function(obj) {
		console.log(obj);
		var content_url = order_detail_url + '?order_no=' + obj.data.order_nos,
			get_site_id = 117;
		if (get_site_id != undefined && get_site_id != '') {
			content_url = content_url + '&site_id=' + get_site_id;
		}
		var node_obj = document.elementFromPoint(event.clientX, event.clientY);
		if ($(node_obj).hasClass('layui-icon-ok') == false) {
			layer.open({
				type: 2,
				anim: -1,
				title: '订单详情',
				closeBtn: true,
				offset: 'r',
				shade: 0.3,
				move: false,
				shadeClose: false,
				area: '90%',
				skin: "layui-anim layui-anim-rl layui-layer-adminRight",
				content: content_url
			})
		}
		return false;
	})
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		const_where[name] = value;
		table.reload(table_filter, {
			where: const_where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	var select_search_list = function(field, value) {
		var where = {};
		where[field] = value;
		const_where[field] = value;
		table.reload(table_filter, {
			where: const_where,
			page: {
				curr: 1
			}
		});
	};
	// select 查询
	form.on('select(builder_type)', function(data) {
		select_search_list('builder_type', data.value);
	})
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	});

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var invoice_id = data.invoice_id;
		console.log(data);
		switch (e) {
			case 'viewInvoice':
				console.log(data.caigou_invoice_pz_info);
				let html = '';
				data.caigou_invoice_pz_info.forEach(function(x, y) {
					html = html + '<div><a href="' + x.save_path +
						'" target="_blank" style="width:120px; border-bottom:1px solid #666666">查看文件' +
						(y + 1) + '</a></div>';
				})
				$('#view-pic-box').html(html);
				layer.open({
					type: 1,
					content: $('#view_pic'),
					shade: false,
					title: '查看文件',
					area: '400px'
				})
				return false;
				layer.open({
					type: 2,
					anim: -1,
					title: '发票明细',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '70%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: data.invoice_proof.save_path
				})
				break;
			case 'recieve_view':
				// 发票明细
				layer.open({
					type: 2,
					anim: -1,
					title: '发票明细',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '70%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: invoice_view_url + '?invoice_id=' + invoice_id
				})
				return false;
				break;
			case 'recieve_del':
				layer.confirm('确定删除信息吗？', function(index) {

					send_info(invoice_ajax_delete_url, {
						invoice_id: invoice_id
					}, function(data) {
						if (data.code == 0) {
							table.reload('invoice_list');
							layer.msg('删除成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'get_code':
				// 获取发票领取码
				var post_data = {
					invoice_id: invoice_id
				};
				send_info(invoice_verify, post_data, function(data) {
					layer.open({
						title: '发票领取码',
						content: '<div class="view-code">' + data.data + '</div>'
					});
				})
				break;
			case 'post_invoice':
				// 配送发票
				var post_data = {
					invoice_id: invoice_id,
					need_post_info: 'yes'
				};
				send_info(invoice_find_url, post_data, function(data) {
					$('form[lay-filter="invoice_express"] input[name="invoice_id"]').val(
						invoice_id);
					$('form[lay-filter="invoice_express"] input[name="post_addr"]').val(data
						.data.shopping_info.street);
					$('form[lay-filter="invoice_express"] input[name="post_realname"]').val(data
						.data.shopping_info.contacts);
					$('form[lay-filter="invoice_express"] input[name="post_phone"]').val(data
						.data.shopping_info.phone);
					$('form[lay-filter="invoice_express"] input[name="express_no"]').val(data
						.data.init_express_no);
					layer.open({
						content: $('#express'),
						type: 1,
						anim: 2,
						title: '配送发票',
						closeBtn: true,
						shadeClose: true,
						area: ['650px', '510px'],
						shade: 0
					})
				})

				break;
			case 'recieve_invoice':
				// 发票已收货
				var post_data = {
					invoice_id: invoice_id,
					field: 'receive_state',
					value: 1
				};
				layer.confirm('确认该发票客户已收到吗?', {
					icon: 3,
					title: '确认信息'
				}, function(index) {
					send_info(invoice_save_invoice_url, post_data, function(data) {
						layer.closeAll();
						layui.table.reload('invoice_list');
						layer.msg('配送成功', {
							icon: 1,
							anim: 6
						})
					})
				})
				break;
			default:
				break;
		}
	})
	// 确认配送
	form.on('submit(submit_express)', function(data) {
		var field = data.field;
		field['field'] = 'post_state';
		field['value'] = 1;
		var url = $('form[lay-filter="invoice_express"]').attr('action');
		layer.confirm('确认配送该发票吗?', {
			icon: 3,
			title: '确认信息'
		}, function(index) {
			send_info(url, field, function(data) {
				layer.closeAll();
				if (data.code == 0) {
					layui.table.reload('invoice_list');
					layer.msg('配送成功', {
						icon: 1,
						anim: 6
					})
				} else {
					layer.confirm(data.msg, {
						icon: 2,
						title: '配送失败'
					})
				}
			})
		})

	})
})
